//
//  InstallTimeView.m
//  TaiHao
//
//  Created by rzm on 16/6/6.
//  Copyright © 2016年 rzm. All rights reserved.
//

#import "InstallTimeView.h"

@interface InstallTimeView ()

@end

@implementation InstallTimeView
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame: frame]) {
        [self createBtn];
        
    }
    return self;
}

- (void)createBtn
{
    _upBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _upBtn.backgroundColor = [UIColor redColor];
    [self addSubview:_upBtn];
    _upBtn.frame = CGRectMake(0, 0, self.width, self.height/3);
    [_upBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_upBtn setImage:[UIImage imageNamed:@"up"] forState:UIControlStateNormal];
    _upBtn.tag = THTTimeViewButtonClickType_Up;
    _timeLab = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_upBtn.frame), self.width, self.height/3)];
    [self addSubview:_timeLab];
    _timeLab.textAlignment = NSTextAlignmentCenter;
    
    _downBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _downBtn.backgroundColor = [UIColor greenColor];
    [self addSubview:_downBtn];
    [_downBtn setImage:[UIImage imageNamed:@"down"] forState:UIControlStateNormal];
    _downBtn.tag = THTTimeViewButtonClickType_Down;
    [_downBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    //    _tTitle = [NSString stringWithFormat:@"%ld",(long)count];
    //    self.timeLab.text = _tTitle;
    
    _downBtn.frame = CGRectMake(0, CGRectGetMaxY(_timeLab.frame) , self.width, self.height/3);
}

- (void)setTimeTitle:(NSString *)timeTitle
{
    _timeTitle = timeTitle;
    self.timeLab.text = _timeTitle;
    _count = [_timeTitle integerValue];
}
- (void)btnClick:(UIButton *)btn
{
    if (btn.tag == THTTimeViewButtonClickType_Up) {
        _count ++;
        
    } else
    {
        _count --;
    }
    
    if (self.tag == THTimeViewButtonType_Hour) {
        if (_count < 0) {
            _count = 23;
        } else if (_count > 23)
        {
            _count = 0;
            
        }
        
        [self setTimeFormat];
    } else if (self.tag == THTimeViewButtonType_Min){
        if (_count >= 60) {
            _count = 0;
        } else if (_count < 0) {
            
            _count = 59;
        }
        [self setTimeFormat];
        
    }else
    {
        if (_count >= 20) {
            _count = 0;
        } else if (_count < 0) {
            
            _count = 19;
        }
        self.timeTitle = [NSString stringWithFormat:@"%ld",(long)_count];
        
    }
    if (self.sendBlock) {
        self.sendBlock();
    }
}

- (void)setTimeFormat
{
    if (_count < 10) {
        self.timeTitle = [NSString stringWithFormat:@"0%ld",(long)_count];
        //        self.timeLab.text = [NSString stringWithFormat:@"0%ld",(long)_count];
    }else
    {
        self.timeTitle = [NSString stringWithFormat:@"%ld",(long)_count];
        //        self.timeLab.text = [NSString stringWithFormat:@"%ld",(long)_count];
    }
    
}


@end
